package Analizer;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JScrollPane;

import java.awt.Cursor;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.GridLayout;
import java.awt.Font;

import javax.swing.border.MatteBorder;

import java.awt.Color;

import javax.swing.JScrollBar;
import javax.swing.BoxLayout;


public abstract class i_OknoAnalizer extends JFrame implements iInterf_Onko{

	private JPanel contentPane;
	public JButton ic_StartStop;
	public JButton ic_Odtworz;
	public JLabel ic_dlRuchu;
	public JLabel ic_speed;
	public JLabel ic_infoZapisu;
	public JSpinner ic_spinner;
	public JPanel ic_Rysuj;
	public Vector<i_Punkt> i_Krzywa;// pojedyncza krzywa
	public Vector<iElement> iVSchemList;//lista Vektora
	
	JScrollPane iJScrollPane;

	private Box horizontalBox;
	private Component rigidArea_1;
	
	private JLabel label;
	private JLabel label_1;
	private JPanel panel1;
	private JPanel panel_5;
	private JLabel lblNewLabel;
	private JLabel lblSpeed;
	private JLabel label_2;
	private JButton btnOdtwrz;
	private JPanel panel_1;
	public final JSpinner i_SpinnerSchemList = new JSpinner();
	SpinnerNumberModel snm;
	public final JLabel i_SizeSchem = new JLabel("");
	protected int i_SpinnerSchemListValue;
	private JButton btnOdczyaj;
	protected JPanel i_PanelSchemLista;

	
	//public JList<String> listaKrzych;//http://stackoverflow.com/questions/13621261/add-a-jlist-to-a-jscrollpane
	//http://stackoverflow.com/questions/14615888/list-of-jpanels-that-eventually-uses-a-scrollbar




	/**
	 * Create the frame.
	 */
	public i_OknoAnalizer() {
		setTitle("Analizer Mouse");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 447, 300);
		setLocationRelativeTo(null);
		
		iVSchemList = new Vector<iElement>(5,10);
				
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
			panel.setBounds(10, 104, 213, 152);
		
		ic_Rysuj = new wyswietlzawartosc();
		FlowLayout flowLayout = (FlowLayout) ic_Rysuj.getLayout();
			ic_Rysuj.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			ic_Rysuj.setBounds(10, 62, 150, 153);
		
		JPanel panel_2 = new JPanel();
			panel_2.setBounds(287, 5, 141, 251);
			contentPane.setLayout(null);
			contentPane.add(ic_Rysuj);
		
		panel_5 = new JPanel();
		panel_5.setBounds(188, 38, 239, 13);
		panel_5.setOpaque(false);
		contentPane.add(panel_5);
		panel_5.setLayout(null);
		
		lblSpeed = new JLabel("interval");
		lblSpeed.setBounds(25, 5, 26, 9);
		lblSpeed.setFont(new Font("Lucida Grande", Font.PLAIN, 7));
		panel_5.add(lblSpeed);
		
		lblNewLabel = new JLabel("odcinek");
		lblNewLabel.setBounds(100, 5, 26, 9);
		lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 7));
		panel_5.add(lblNewLabel);
		
		label_2 = new JLabel("speed");
		label_2.setBounds(146, 5, 20, 9);
		label_2.setFont(new Font("Lucida Grande", Font.PLAIN, 7));
		panel_5.add(label_2);
		
		JPanel panel_3 = new JPanel();
			panel_3.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			panel_3.setBounds(10, 10, 420, 43);
			contentPane.add(panel_3);
		
		ic_StartStop = new JButton("PLAY");
		ic_StartStop.setBounds(7, 8, 75, 29);
			ic_StartStop.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if_StartStop();
				}
			});
			panel_3.setLayout(null);
			panel_3.add(ic_StartStop);
		
		ic_Odtworz = new JButton("Odtw\u00F3rz");
		ic_Odtworz.setBounds(87, 8, 97, 29);
		ic_Odtworz.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					if_Odtworz(i_SpinnerSchemListValue);
				} catch (AWTException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
			panel_3.add(ic_Odtworz);
		
		//SpinnerNumberModel model1 = new SpinnerNumberModel(10, 0, 100, 5);
			ic_spinner = new JSpinner (new SpinnerNumberModel(1, 1, 50, 1));
			ic_spinner.setBounds(189, 7, 65, 28);
			ic_spinner.addPropertyChangeListener(new PropertyChangeListener() {
				public void propertyChange(PropertyChangeEvent evt) {
					if_Spinner();// przy starcie wpisuje zmiennej wartosc
				}
			});
			ic_spinner.addChangeListener(new ChangeListener() {
				@Override
				public void stateChanged(ChangeEvent e) {
					if_Spinner(); // przy aminaie wiisuje zmiennej i_valSpinner wartosc
				}
			});
				panel_3.add(ic_spinner);

							Component rigidArea = Box.createRigidArea(new Dimension(20, 20));
							rigidArea.setBounds(260, 10, 10, 20);
							rigidArea.setPreferredSize(new Dimension(10, 20));
							panel_3.add(rigidArea);
		
		ic_dlRuchu = new JLabel("0000");
		ic_dlRuchu.setBounds(274, 13, 32, 16);
			panel_3.add(ic_dlRuchu);
											
							horizontalBox = Box.createHorizontalBox();
							horizontalBox.setBounds(311, 22, 0, 0);
							panel_3.add(horizontalBox);
		
							Component rigidArea_2 = Box.createRigidArea(new Dimension(10, 20));
							rigidArea_2.setBounds(306, 10, 5, 20);
							rigidArea_2.setEnabled(false);
							panel_3.add(rigidArea_2);
			
		ic_speed = new JLabel("0000");
		ic_speed.setBounds(319, 12, 32, 16);
			panel_3.add(ic_speed);
		
							rigidArea_1 = Box.createRigidArea(new Dimension(20, 20));
							rigidArea_1.setBounds(351, 9, 5, 20);
							panel_3.add(rigidArea_1);
		
		ic_infoZapisu = new JLabel("000"); //wyswietla aktualna ilosc krzywych w bibliotece
		ic_infoZapisu.setBounds(366, 14, 17, 16);
			panel_3.add(ic_infoZapisu);
		
			
		JPanel iPanelListy = new JPanel();// 1. panel do ktorego ladowana jest lista
			iPanelListy.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			iPanelListy.setBounds(172, 60, 258, 153);
			iPanelListy.setLayout(null);
		contentPane.add(iPanelListy);
			
		i_PanelSchemLista = new JPanel();// 2. panel do ktorego jest dodawany Elment()
		i_PanelSchemLista.setBounds(172, 60, 258, 153);
		i_PanelSchemLista.setLayout(new GridBagLayout());
			//GridBagConstraints gbc = new GridBagConstraints();
           // gbc.gridwidth = GridBagConstraints.REMAINDER;
           // gbc.weightx = 1;
          //  gbc.weighty = 1;
	   // i_PLista.add(new JPanel(), gbc);
		//contentPane.add(i_mojaLista);
		iPanelListy.add(i_PanelSchemLista);
	    

		iJScrollPane = new JScrollPane(i_PanelSchemLista); //3.dodaja do Ilisty scroll z lista ic_mojaLista
		iJScrollPane.setBounds(6, 6, 246, 141);
		iPanelListy.add(iJScrollPane);
		
			
		//klawisze na dole
		panel1 = new JPanel(); 
			panel1.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			panel1.setBounds(172, 216, 255, 34);
		contentPane.add(panel1);
		
		btnOdtwrz = new JButton("Zapisz");
		btnOdtwrz.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					iZapiszDoPlikuSchemat();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
		panel1.add(btnOdtwrz);
		
		btnOdczyaj = new JButton("Odczyaj");
		btnOdczyaj.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					iLadujZPlikuSchemat();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		panel1.add(btnOdczyaj);
		
		JButton btnUsu = new JButton("Usu\u0144");
		panel1.add(btnUsu);
		
		panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(10, 216, 150, 34);
		contentPane.add(panel_1);
		
		
		{	
			snm = new SpinnerNumberModel(0,0, (iVSchemList.size()), 1);
			i_SpinnerSchemList.setToolTipText("0000");
			i_SpinnerSchemList.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent arg0){
					i_SpinnerSchemListValue = ((int) i_SpinnerSchemList.getValue()-1);
					System.out.println("spiner pozycja " + i_SpinnerSchemListValue);
					}
			});
			i_SpinnerSchemList.setModel(snm);
			panel_1.add(i_SpinnerSchemList);
		}
		{
			panel_1.add(i_SizeSchem);
			
	    	if (iVSchemList.isEmpty()){
	    		i_SizeSchem.setText("-" +" lp.");
	    		snm.setMaximum(0);
	    	} 
		}
		
	}
}


